package sktit.practice.jpaint.draw;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import sktit.practice.jpaint.canvas.Canvas;
import sktit.practice.jpaint.canvas.EllipseCanvas;
import sktit.practice.jpaint.shape.Ellipse;
import sktit.practice.jpaint.util.ShapeUtils;

public class EllipseDraw extends AbstractDraw {

	public EllipseDraw(Shell aShell, Display aDisplay, Color aColor, int aWidth,
			int aStyle)
	{
		super(aShell, aDisplay, aColor, aWidth, aStyle);
		mouseMoveListener = new MouseMoveListener(){

			@Override
			public void mouseMove(MouseEvent arg0) {
				if(isDrawing)
				{
					GC gc = createGC();
					gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
					Rectangle border = ShapeUtils.generate(begin, temp);
					gc.drawOval(border.x, border.y, border.width, border.height);
					gc.setForeground(getColorBorder());
					border = ShapeUtils.generate(new Point(arg0.x, arg0.y), begin);
					gc.drawOval(border.x, border.y, border.width, border.height);
					canvasManager.draw();
					gc.dispose();
					temp.x = arg0.x;
					temp.y = arg0.y;
					
				}
			}
			
		};
		
		mouseListener = new MouseListener() {
			
			@Override
			public void mouseUp(MouseEvent arg0) {
				
				vertex.add(new Point(Math.min(begin.x, arg0.x), Math.min(begin.y, arg0.y)));
				
				Canvas ellipseCanvas = new EllipseCanvas(createGC(), 
						new Ellipse(vertex.get(0), Math.abs(begin.x - arg0.x), Math.abs(begin.y - arg0.y)));
				canvasManager.add(ellipseCanvas);
				vertex.clear();
				isDrawing = false;
				
			}
			
			@Override
			public void mouseDown(MouseEvent arg0) {
				if (!isDrawing)
				{
					begin = new Point(arg0.x, arg0.y);
					temp = new Point(arg0.x, arg0.y);
					isDrawing = true;
				}
				
			}
			
			@Override
			public void mouseDoubleClick(MouseEvent arg0) {
				
			}
		};
	
		shell.addMouseMoveListener(mouseMoveListener);
		shell.addMouseListener(mouseListener);
	}

}
